package com.sm.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sm.controller.request.TeacherRegisterRequest;
import com.sm.controller.request.UpdatePasswordRequest;
import com.sm.controller.request.UserLoginRequest;
import com.sm.dao.CourseMapper;
import com.sm.dao.TeacherMapper;
import com.sm.dao.UserMapper;
import com.sm.entity.Course;
import com.sm.entity.Teacher;
import com.sm.entity.User;
import com.sm.exception.CodeEnum;
import com.sm.service.UserService;
import com.sm.utils.SaTokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Cola
 * @description
 * @date 2022/2/1
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Resource
    TeacherMapper teacherMapper;

    @Resource
    UserMapper userMapper;

    @Resource
    CourseMapper courseMapper;


    @Override
    public void login(UserLoginRequest userLoginRequest){
        String username = userLoginRequest.getUsername();
        String password = userLoginRequest.getPassword();
        // 从数据库中获得该用户名对应的密码
        User user = userMapper.getUserByUserName(username);
        // 若user为空，则返回错误信息"用户名不存在"
        CodeEnum.username_not_exist.assertIsNotNull(user);
        String passwordInDB = user.getPassword();
        Integer userId = user.getId();
        // 校验密码是否正确
        if(password.equals(passwordInDB)){
            StpUtil.login(userId);
        }else{
            CodeEnum.wrong_password.throwException();
        }

    }

    @Override
    public void updatePassword(UpdatePasswordRequest updatePasswordRequest) throws Exception {
        Integer userId = SaTokenUtils.getUserId();
        String originPassword = updatePasswordRequest.getOriginPassword();
        String passwordInDB = userMapper.getPassword(userId);
        CodeEnum.wrong_password.assertIsEquals(passwordInDB,originPassword);
        // 原密码正确则修改密码
        String newPassword = updatePasswordRequest.getNewPassword();
        int res = userMapper.updatePassword(userId,newPassword);
        CodeEnum.fail_update_password.assertIsEquals(res,1);
    }


    @Override
    public void addTeacherUser(TeacherRegisterRequest request) throws Exception {
        userMapper.addTeacher(new User(null, request.getUsername(), request.getPassword(), request.getRealName(), request.getPhone(),request.getGender(),request.getAddress(),"teacher"));
    }

    @Override
    public void updateTeacherUser(Teacher teacher) throws Exception {
        User user = userMapper.selectById(teacher.getId());
        user.setAddress(teacher.getAddress());
        user.setPhone(teacher.getPhone());
        user.setUsername(teacher.getUsername());
        user.setRealName(teacher.getName());
        userMapper.updateById(user);
    }

    @Override
    public User getUser() throws Exception {
        Integer userId = SaTokenUtils.getUserId();
        User user = userMapper.selectById(userId);
        CodeEnum.fail_get_user_info.assertIsNotNull(user);
        return user;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteTeacher() throws Exception {
        User user = getUser();
        userMapper.deleteById(user.getId());
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",user.getUsername());
        teacherMapper.delete(queryWrapper);
        QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>();
        courseQueryWrapper.eq("teacher_id",user.getId());
        courseMapper.delete(courseQueryWrapper);


    }
}
